package com.yic.module.device.controller.admin.faulthandle;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.common.pojo.CommonResult;
import static com.yic.framework.common.pojo.CommonResult.success;

import com.yic.framework.excel.core.util.ExcelUtils;

import com.yic.framework.operatelog.core.annotations.OperateLog;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.yic.module.device.controller.admin.faulthandle.vo.*;
import com.yic.module.device.dal.dataobject.faulthandle.FaultHandleDO;
import com.yic.module.device.convert.faulthandle.FaultHandleConvert;
import com.yic.module.device.service.faulthandle.FaultHandleService;

@Tag(name = "管理后台 - 设备报修处理")
@RestController
@RequestMapping("/device/fault-handle")
@Validated
public class FaultHandleController {

    @Resource
    private FaultHandleService faultHandleService;

    @PostMapping("/create")
    @Operation(summary = "创建设备报修处理")
    @PreAuthorize("@ss.hasPermission('device:fault-handle:create')")
    public CommonResult<Long> createFaultHandle(@Valid @RequestBody FaultHandleCreateReqVO createReqVO) {
        return success(faultHandleService.createFaultHandle(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备报修处理")
    @PreAuthorize("@ss.hasPermission('device:fault-handle:update')")
    public CommonResult<Boolean> updateFaultHandle(@Valid @RequestBody FaultHandleUpdateReqVO updateReqVO) {
        faultHandleService.updateFaultHandle(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备报修处理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('device:fault-handle:delete')")
    public CommonResult<Boolean> deleteFaultHandle(@RequestParam("id") Long id) {
        faultHandleService.deleteFaultHandle(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备报修处理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('device:fault-handle:query')")
    public CommonResult<FaultHandleRespVO> getFaultHandle(@RequestParam("id") Long id) {
        FaultHandleDO faultHandle = faultHandleService.getFaultHandle(id);
        return success(FaultHandleConvert.INSTANCE.convert(faultHandle));
    }

    @GetMapping("/list")
    @Operation(summary = "获得设备报修处理列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('device:fault-handle:query')")
    public CommonResult<List<FaultHandleRespVO>> getFaultHandleList(@RequestParam("ids") Collection<Long> ids) {
        List<FaultHandleDO> list = faultHandleService.getFaultHandleList(ids);
        return success(FaultHandleConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备报修处理分页")
    @PreAuthorize("@ss.hasPermission('device:fault-handle:query')")
    public CommonResult<PageResult<FaultHandleRespVO>> getFaultHandlePage(@Valid FaultHandlePageReqVO pageVO) {
        PageResult<FaultHandleDO> pageResult = faultHandleService.getFaultHandlePage(pageVO);
        return success(FaultHandleConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备报修处理 Excel")
    @PreAuthorize("@ss.hasPermission('device:fault-handle:export')")
    @OperateLog(type = EXPORT)
    public void exportFaultHandleExcel(@Valid FaultHandleExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<FaultHandleDO> list = faultHandleService.getFaultHandleList(exportReqVO);
        // 导出 Excel
        List<FaultHandleExcelVO> datas = FaultHandleConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "设备报修处理.xls", "数据", FaultHandleExcelVO.class, datas);
    }

}
